//
//  WXPAuthIncomeContentView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/2.
//
//

#import "WXPAuthIncomeContentView.h"

@interface WXPAuthIncomeContentView ()<UITextFieldDelegate>

@end

@implementation WXPAuthIncomeContentView

#pragma mark - lazy
- (UIView *)viewContent{
    if (!_viewContent) {
        _viewContent = [UIView new];
        _viewContent.backgroundColor = COLOR_WHITE;
        _viewContent.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return _viewContent;
}
// 工作类型
- (WXPAuthSelectView *)jobTypeView
{
    if (_jobTypeView == nil) {
        _jobTypeView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Estado de empleo";
        [_jobTypeView resetViewWithModel:model];
        WEAKSELF
        _jobTypeView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Work_Status action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Work_Status_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.jobTypeAry.count; i ++) {
                NSDictionary * dic = weakSelf.jobTypeAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:1];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _jobTypeView;
}
// 行业类型
- (WXPAuthSelectView *)industryView
{
    if (_industryView == nil) {
        _industryView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Sector";
        [_industryView resetViewWithModel:model];
        WEAKSELF
        _industryView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Sector action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Sector_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.industryAry.count; i ++) {
                NSDictionary * dic = weakSelf.industryAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:2];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _industryView;
}
// 公司名称（可选）
- (WXPAuthSelectView *)tfCompanyName
{
    if (_tfCompanyName == nil) {
        _tfCompanyName = [WXPAuthSelectView  new];
        _tfCompanyName.imageArrow.hidden = true;
        _tfCompanyName.tfContent.userInteractionEnabled = true;
        ModelBtn * model = [ModelBtn new];
        model.title = @"Nombre de la empresa(Opcional)";
        model.content = @"Por favor escribe";
        [_tfCompanyName resetViewWithModel:model];
        _tfCompanyName.tfContent.delegate = self;
        WEAKSELF
        _tfCompanyName.blockTextFieldDidChange = ^{
            [weakSelf resetEntregarBtn];
        };
    }
    return  _tfCompanyName;
}
// 公司地址
- (WXPAuthSelectView *)tfCompanyAddress
{
    if (_tfCompanyAddress == nil) {
        _tfCompanyAddress = [WXPAuthSelectView  new];
        _tfCompanyAddress.imageArrow.hidden = true;
        _tfCompanyAddress.tfContent.userInteractionEnabled = true;
        ModelBtn * model = [ModelBtn new];
        model.title = @"Dirección de la empresa";
        model.content = @"Por favor escribe";
        [_tfCompanyAddress resetViewWithModel:model];
        _tfCompanyAddress.tfContent.delegate = self;
        WEAKSELF
        _tfCompanyAddress.blockTextFieldDidChange = ^{
            [weakSelf resetEntregarBtn];
        };
    }
    return  _tfCompanyAddress;
}
// 公司电话（可选）
- (WXPAuthSelectView *)tfCompanyPhone
{
    if (_tfCompanyPhone == nil) {
        _tfCompanyPhone = [WXPAuthSelectView  new];
        _tfCompanyPhone.imageArrow.hidden = true;
        _tfCompanyPhone.tfContent.userInteractionEnabled = true;
        _tfCompanyPhone.tfContent.keyboardType = UIKeyboardTypePhonePad;
        ModelBtn * model = [ModelBtn new];
        model.title = @"Número teléfono de la empresa(Opcional)";
        model.content = @"Por favor escribe";
        [_tfCompanyPhone resetViewWithModel:model];
        _tfCompanyPhone.tfContent.delegate = self;
    }
    return  _tfCompanyPhone;
}
// 发薪类型
- (WXPAuthSelectView *)salaryTypeView
{
    if (_salaryTypeView == nil) {
        _salaryTypeView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Tipo de salario";
        [_salaryTypeView resetViewWithModel:model];
        WEAKSELF
        _salaryTypeView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Method action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Salary_Method_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.salaryTypeAry.count; i ++) {
                NSDictionary * dic = weakSelf.salaryTypeAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:3];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _salaryTypeView;
}
// 发薪日1
- (WXPAuthSelectView *)salaryDay1View
{
    if (_salaryDay1View == nil) {
        _salaryDay1View = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Día de pago de salario 1";
        [_salaryDay1View resetViewWithModel:model];
        WEAKSELF
        _salaryDay1View.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Day1 action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Salary_Day1_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 1; i <= 31; i ++) {
                ModelBtn * model = [ModelBtn new];
                model.title = [NSString stringWithFormat:@"%d",i];
                model.content = [NSString stringWithFormat:@"%d",i];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:4];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _salaryDay1View;
}
// 发薪日2
- (WXPAuthSelectView *)salaryDay2View
{
    if (_salaryDay2View == nil) {
        _salaryDay2View = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Día de pago de salario 2(Opcional)";
        [_salaryDay2View resetViewWithModel:model];
        WEAKSELF
        _salaryDay2View.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Day2 action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Salary_Day2_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 1; i <= 31; i ++) {
                ModelBtn * model = [ModelBtn new];
                model.title = [NSString stringWithFormat:@"%d",i];
                model.content = [NSString stringWithFormat:@"%d",i];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:5];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _salaryDay2View;
}
// 月收入
- (WXPAuthSelectView *)monthlyIncomeView
{
    if (_monthlyIncomeView == nil) {
        _monthlyIncomeView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Ingreso mensual";
        [_monthlyIncomeView resetViewWithModel:model];
        WEAKSELF
        _monthlyIncomeView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Monthly_Income action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Monthly_Income_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.monthlyIncomeAry.count; i ++) {
                NSDictionary * dic = weakSelf.monthlyIncomeAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:6];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _monthlyIncomeView;
}
- (WXPAuthPickerView *)pickerView
{
    if (_pickerView == nil) {
        _pickerView = [WXPAuthPickerView  new];
        WEAKSELF
        _pickerView.blockDidSelectRow = ^(ModelBtn *model, NSInteger tag) {
            if(tag == 1){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Work_Status action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.jobTypeView.tfContent.text = model.title;
                    weakSelf.jobStatus = model.content;
                }
            }else if(tag == 2){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Sector action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.industryView.tfContent.text = model.title;
                    weakSelf.industry = model.content;
                }
            }else if(tag == 3){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Method action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.salaryTypeView.tfContent.text = model.title;
                    weakSelf.salaryType = model.content;
                }
            }else if(tag == 4){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Day1 action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.salaryDay1View.tfContent.text = model.title;
                    weakSelf.salaryDay1 = model.content;
                }
            }else if(tag == 5){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Salary_Day2 action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.salaryDay2View.tfContent.text = model.title;
                    weakSelf.salaryDay2 = model.content;
                }
            }else if(tag == 6){
                [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Monthly_Income action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.monthlyIncomeView.tfContent.text = model.title;
                    weakSelf.monthlyIncome = model.content;
                }
            }
            [weakSelf resetEntregarBtn];
        };
    }
    return  _pickerView;
}
- (UIView *)viewBottomTips{
    if (_viewBottomTips == nil) {
        _viewBottomTips = [UIView  new];
        _viewBottomTips.widthHeight = XY(self.viewContent.width, 0);
        _viewBottomTips.backgroundColor = [UIColor clearColor];
        UILabel * title = [UILabel new];
        title.fontNum = 12;
        title.numberOfLines = 0;
        title.lineSpace = 3;
        title.textColor = COLOR_RED;
        [title fitTitle:@"*Cargue los siguientes datos para mejorar su evaluación crediticia" variable:(_viewBottomTips.width-2*LEFT_MARDIN)];
        title.leftTop = XY(LEFT_MARDIN, Ratio(12));
        [_viewBottomTips addSubview:title];
        _viewBottomTips.height = title.bottom;
    }
    return  _viewBottomTips;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self configUI];
    }
    return self;
}

- (void)configUI{
    self.viewContent = [UIView initWithViews:@[
        self.jobTypeView,
        self.industryView,
        self.tfCompanyName,
        self.tfCompanyAddress,
        self.tfCompanyPhone,
        self.salaryTypeView,
        self.salaryDay1View,
        self.salaryDay2View,
        self.monthlyIncomeView,
        self.viewBottomTips,
        [GlobalMethod getViewWithHeight:Ratio(16)],
    ] width:SCREEN_WIDTH-2*LEFT_MARDIN];
    self.viewContent.backgroundColor = COLOR_WHITE;
    [GlobalMethod setRoundView:self.viewContent color:[UIColor clearColor] numRound:16 width:0];
    self.viewContent.centerXTop = XY(self.width/2, 0);
    [self addSubview:self.viewContent];
    self.height = self.viewContent.bottom+Ratio(20);
}

#pragma mark - textField delegate
// 获得焦点
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.tfCompanyName.tfContent){
        // 公司名称
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Name_Input action:KEY_GET_FOCUS type:KEY_input];
        [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Company_Name_Focus extInfo:nil];
    }
    if (textField == self.tfCompanyPhone.tfContent){
        // 公司号码
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Phone_Input action:KEY_GET_FOCUS type:KEY_input];
        [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Company_Phone_Focus extInfo:nil];
    }
    if (textField == self.tfCompanyAddress.tfContent){
        // 公司地址
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Address_Input action:KEY_GET_FOCUS type:KEY_input];
        [GlobalMethod hitPointWithPageName:KEY_VerifyIncome action:KEY_Company_Address_Focus extInfo:nil];
    }
}
// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if(textField == self.tfCompanyName.tfContent){
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Name_Input action:KEY_LOST_FOCUS type:KEY_input];
    }
    if(textField == self.tfCompanyPhone.tfContent){
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Phone_Input action:KEY_LOST_FOCUS type:KEY_input];
    }
    if (textField == self.tfCompanyAddress.tfContent){
        [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Address_Input action:KEY_LOST_FOCUS type:KEY_input];
    }
}

// 粘贴行为
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(textField == self.tfCompanyName.tfContent){
        if (isStr(string)) {
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Name_Input action:KEY_INPUT type:KEY_input];
        }
    }
    if(textField == self.tfCompanyPhone.tfContent){
        if (isStr(string)) {
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Phone_Input action:KEY_INPUT type:KEY_input];
        }
    }
    if(textField == self.tfCompanyAddress.tfContent){
        if (isStr(string)) {
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Address_Input action:KEY_INPUT type:KEY_input];
        }
    }
    // 检测是否为粘贴行为
    NSString * trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([UIPasteboard generalPasteboard].string.length > 0 && [trimmedString isEqualToString:[UIPasteboard generalPasteboard].string]) {
        if(textField == self.tfCompanyName.tfContent){
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Name_Input action:KEY_PARSE type:KEY_input];
        }
        if(textField == self.tfCompanyPhone.tfContent){
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Phone_Input action:KEY_PARSE type:KEY_input];
        }
        if(textField == self.tfCompanyAddress.tfContent){
            [GlobalMethod buryPointWithPageName:KEY_VerifyIncome featureName:KEY_Company_Address_Input action:KEY_PARSE type:KEY_input];
        }
        return true;
    }
    return YES;
}


// 重置按钮状态
- (void)resetEntregarBtn{
    BOOL nameOnlySpace = [self.tfCompanyAddress.tfContent.text isOnlyBlankSpace];
    
    if(isStr(self.jobTypeView.tfContent.text) &&
       isStr(self.industryView.tfContent.text) &&
       isStr(self.tfCompanyAddress.tfContent.text) &&
       nameOnlySpace == false &&
       isStr(self.salaryTypeView.tfContent.text) &&
       isStr(self.salaryDay1View.tfContent.text) &&
       isStr(self.monthlyIncomeView.tfContent.text)){
        if(self.blockResetAuthBtn){
            self.blockResetAuthBtn(true);
        }
    }else{
        if(self.blockResetAuthBtn){
            self.blockResetAuthBtn(false);
        }
    }
}

@end
